﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using YJKStairs.CommonUnit;
using YJKStairs.DataInfos;
using YJKStairs.Manage;

namespace YJKStairs.Kit
{
	public class ClipperStairsKit : ProjectionDrawingKit
	{
		public ClipperStairsKit(Autodesk.Revit.ApplicationServices.Application app) : base(app)
		{
		}

		public bool ClipperStairsProfileCurves(ref ClipperStairs stairs, bool bDrawNoteInfo, int basePointIndex, bool lrMirror, bool tbMirror, bool bLeftSideUp, double rotateAngle, bool bBottom, bool bMid)
		{
			bool result = true;
			LineRunData leftRunData = stairs.m_LeftRunData;
			LineRunData rightRunData = stairs.m_RightRunData;
			double width = stairs.m_LandData.Width;
			double dStairsWidth = stairs.m_dStairsWidth;
			double bundingBoxHeight = (stairs.m_LandData.TheType == LandingType.LT_None) ? leftRunData.RunLength : (leftRunData.RunLength + 2.0 * width);
			List<XYZ> list = new List<XYZ>();
			XYZ zero = XYZ.Zero;
			XYZ xyz = new XYZ(leftRunData.RunWidth, 0.0, 0.0);
			XYZ xyz2 = new XYZ(0.0, leftRunData.RunLength, 0.0);
			XYZ xyz3 = new XYZ(leftRunData.RunWidth, leftRunData.RunLength, 0.0);
			double stairsWellWidth = stairs.GetStairsWellWidth();
			XYZ xyz4 = new XYZ(stairsWellWidth, 0.0, 0.0);
			XYZ xyz5 = new XYZ(stairsWellWidth + rightRunData.RunWidth, 0.0, 0.0);
			XYZ ptRunBottomLeft = xyz + xyz4;
			XYZ xyz6 = xyz + xyz5;
			XYZ ptRunTopLeft = xyz3 + xyz4;
			XYZ xyz7 = xyz3 + xyz5;
			XYZ xyz8 = new XYZ(0.0, width, 0.0);
			XYZ xyz9 = xyz2 + xyz8;
			XYZ xyz10 = xyz7 + xyz8;
			XYZ xyz11 = zero - xyz8;
			XYZ xyz12 = xyz6 - xyz8;
			stairs.m_LeftRunData.PtRunBottomLeft = zero;
			stairs.m_LeftRunData.PtRunBottomRight = xyz;
			stairs.m_LeftRunData.PtRunTopLeft = xyz2;
			stairs.m_LeftRunData.PtRunTopRight = xyz3;
			stairs.m_RightRunData.PtRunBottomLeft = ptRunBottomLeft;
			stairs.m_RightRunData.PtRunBottomRight = xyz6;
			stairs.m_RightRunData.PtRunTopLeft = ptRunTopLeft;
			stairs.m_RightRunData.PtRunTopRight = xyz7;
			stairs.m_listPointsSupport.Add(xyz11);
			stairs.m_listPointsSupport.Add(zero);
			stairs.m_listPointsSupport.Add(xyz2);
			stairs.m_listPointsSupport.Add(xyz9);
			stairs.m_listPointsSupport.Add(xyz10);
			stairs.m_listPointsSupport.Add(xyz7);
			stairs.m_listPointsSupport.Add(xyz6);
			stairs.m_listPointsSupport.Add(xyz12);
			if (stairs.m_LandData.TheType == LandingType.LT_Clipper)
			{
				list.Add(xyz11);
				list.Add(xyz9);
				list.Add(xyz10);
				list.Add(xyz12);
			}
			else
			{
				list.Add(stairs.m_LeftRunData.PtRunBottomLeft);
				list.Add(stairs.m_LeftRunData.PtRunTopLeft);
				list.Add(stairs.m_RightRunData.PtRunTopRight);
				list.Add(stairs.m_RightRunData.PtRunBottomRight);
			}
			XYZ xyz13 = list[basePointIndex];
			base.CalulateTransform(lrMirror, tbMirror, rotateAngle, dStairsWidth, bundingBoxHeight, xyz13);
			stairs.m_TransVector = Geometry.TransformPoint(xyz13, this.m_Transform);
			stairs.m_listOtherCurve.AddRange(base.DrawBasePoint(xyz13, 100.0));
			stairs.m_Transform = this.m_Transform;
			this.DrawClipperStairsRunCurves(ref stairs, bLeftSideUp, bDrawNoteInfo);
			this.DrawClipperStairsLandCurves(ref stairs, bLeftSideUp, xyz9, xyz10, xyz11, xyz12);
			this.DrawClipperStairsBeamCurves(ref stairs, bLeftSideUp, bBottom, bMid);
			this.DrawClipperStairsNoteInfo(ref stairs, bLeftSideUp);
			if (bDrawNoteInfo)
			{
				this.DrawUpStairsNoteInfo(ref stairs, bLeftSideUp);
			}
			return result;
		}

		private void DrawClipperStairsRunCurves(ref ClipperStairs param_stairs, bool bLeftSideUp, bool bDrawNoteInfo)
		{
			if (param_stairs != null)
			{
				LineRunData leftRunData = param_stairs.m_LeftRunData;
				LineRunData rightRunData = param_stairs.m_RightRunData;
				if (bLeftSideUp)
				{
					SingleRunCurveInfo singleRunCurveInfo = new SingleRunCurveInfo();
					singleRunCurveInfo.BottomElevation = leftRunData.BottomElevation;
					base.DrawLinearRun(leftRunData, leftRunData.PtRunBottomLeft, leftRunData.PtRunBottomRight, leftRunData.PtRunTopRight, leftRunData.PtRunTopLeft, ref singleRunCurveInfo, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
					param_stairs.m_listRunCurve.Add(singleRunCurveInfo);
					singleRunCurveInfo = new SingleRunCurveInfo();
					singleRunCurveInfo.BottomElevation = rightRunData.BottomElevation;
					base.DrawLinearRun(rightRunData, rightRunData.PtRunTopRight, rightRunData.PtRunTopLeft, rightRunData.PtRunBottomLeft, rightRunData.PtRunBottomRight, ref singleRunCurveInfo, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
					param_stairs.m_listRunCurve.Add(singleRunCurveInfo);
					return;
				}
				SingleRunCurveInfo singleRunCurveInfo2 = new SingleRunCurveInfo();
				singleRunCurveInfo2.BottomElevation = leftRunData.BottomElevation;
				base.DrawLinearRun(leftRunData, leftRunData.PtRunTopRight, leftRunData.PtRunTopLeft, leftRunData.PtRunBottomLeft, leftRunData.PtRunBottomRight, ref singleRunCurveInfo2, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
				param_stairs.m_listRunCurve.Add(singleRunCurveInfo2);
				singleRunCurveInfo2 = new SingleRunCurveInfo();
				singleRunCurveInfo2.BottomElevation = rightRunData.BottomElevation;
				base.DrawLinearRun(rightRunData, rightRunData.PtRunBottomLeft, rightRunData.PtRunBottomRight, rightRunData.PtRunTopRight, rightRunData.PtRunTopLeft, ref singleRunCurveInfo2, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
				param_stairs.m_listRunCurve.Add(singleRunCurveInfo2);
			}
		}

		private void DrawClipperStairsLandCurves(ref ClipperStairs param_stairs, bool bLeftSideUp, XYZ ptTopLandLeftTop, XYZ ptTopLandRightTop, XYZ ptBottomLandLeftBottom, XYZ ptBottomLandRightBottom)
		{
			if (param_stairs != null && param_stairs.m_LandData.TheType == LandingType.LT_Clipper)
			{
				LineRunData leftRunData = param_stairs.m_LeftRunData;
				LineRunData rightRunData = param_stairs.m_RightRunData;
				double stairsTopLandOffset = ConstValueManager.StairsTopLandOffset;
				XYZ xyz = XYZ.Zero;
				xyz = rightRunData.PtRunTopRight - leftRunData.PtRunTopLeft;
				xyz = xyz.Normalize();
				xyz = Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
				xyz *= stairsTopLandOffset;
				List<CurveInfo> landCurves = base.CreateLandEdgeloop(new List<XYZ>
				{
					leftRunData.PtRunTopLeft + xyz,
					ptTopLandLeftTop,
					ptTopLandRightTop,
					rightRunData.PtRunTopRight + xyz
				});
				double bottomElevation = 0.0;
				LandingCurveInfo item = new LandingCurveInfo(landCurves, bottomElevation);
				new LandingCurveInfo(landCurves, bottomElevation);
				param_stairs.m_listLandCurve.Add(item);
				xyz = leftRunData.PtRunBottomLeft - rightRunData.PtRunBottomRight;
				xyz = xyz.Normalize();
				xyz = Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
				xyz *= stairsTopLandOffset;
				List<CurveInfo> landCurves2 = base.CreateLandEdgeloop(new List<XYZ>
				{
					ptBottomLandLeftBottom,
					leftRunData.PtRunBottomLeft + xyz,
					rightRunData.PtRunBottomRight + xyz,
					ptBottomLandRightBottom
				});
				LandingCurveInfo item2 = new LandingCurveInfo(landCurves2, bottomElevation);
				new LandingCurveInfo(landCurves2, bottomElevation);
				param_stairs.m_listLandCurve.Add(item2);
			}
		}

		private void DrawClipperStairsBeamCurves(ref ClipperStairs param_stairs, bool bLeftSideUp, bool bBottom, bool bMid)
		{
			if (param_stairs != null)
			{
				double num = param_stairs.m_BeamData.BeamWidth / 2.0;
				LineRunData leftRunData = param_stairs.m_LeftRunData;
				LineRunData rightRunData = param_stairs.m_RightRunData;
				XYZ xyz = new XYZ(0.0, num, 0.0);
				XYZ xyz2 = new XYZ(0.0, -num, 0.0);
				XYZ ptRunTopLeft = leftRunData.PtRunTopLeft;
				XYZ ptRunTopRight = rightRunData.PtRunTopRight;
				XYZ ptRunBottomLeft = leftRunData.PtRunBottomLeft;
				XYZ ptRunBottomRight = rightRunData.PtRunBottomRight;
				if (bBottom)
				{
					List<CurveInfo> list = new List<CurveInfo>();
					XYZ ptS = ptRunTopLeft + xyz;
					XYZ ptE = ptRunTopRight + xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, xyz2));
					ptS = ptRunBottomLeft + xyz2;
					ptE = ptRunBottomRight + xyz2;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, xyz));
					double bottomElevation = rightRunData.BottomElevation;
					BeamCurveInfo beamCurveInfo = new BeamCurveInfo(list, bottomElevation);
					beamCurveInfo.m_Type = BeamType.BT_Bottom;
					beamCurveInfo.m_nFloorNum = 0;
					param_stairs.m_listBeamCurve.Add(beamCurveInfo);
				}
				if (bMid)
				{
					List<CurveInfo> list2 = new List<CurveInfo>();
					XYZ ptS2 = ptRunTopLeft + xyz;
					XYZ ptE2 = ptRunTopRight + xyz;
					list2.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS2, ptE2, xyz));
					ptS2 = ptRunBottomLeft + xyz2;
					ptE2 = ptRunBottomRight + xyz2;
					list2.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS2, ptE2, xyz2));
					double bottomElevation2 = rightRunData.BottomElevation;
					BeamCurveInfo beamCurveInfo2 = new BeamCurveInfo(list2, bottomElevation2);
					beamCurveInfo2.m_Type = BeamType.BT_Mid;
					beamCurveInfo2.m_nFloorNum = 0;
					param_stairs.m_listBeamCurve.Add(beamCurveInfo2);
				}
			}
		}

		private void DrawClipperStairsNoteInfo(ref ClipperStairs param_stairs, bool bLeftSideUp)
		{
			bool flag = param_stairs.m_LandData.TheType == LandingType.LT_Clipper;
			bool flag2 = Math.Abs(param_stairs.GetStairsWellWidth()) >= 1E-07;
			LineRunData leftRunData = param_stairs.m_LeftRunData;
			LineRunData rightRunData = param_stairs.m_RightRunData;
			double width = param_stairs.m_LandData.Width;
			XYZ xyz = new XYZ(0.0, width, 0.0);
			XYZ xyz2 = XYZ.BasisY * 2.0;
			XYZ xyz3 = XYZ.BasisX * 2.0;
			XYZ xyz4 = XYZ.Zero;
			XYZ xyz5 = XYZ.Zero;
			xyz4 = leftRunData.PtRunBottomLeft - xyz2;
			xyz5 = leftRunData.PtRunBottomRight - xyz2;
			if (flag)
			{
				xyz4 -= xyz;
				xyz5 -= xyz;
			}
			base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, xyz4, xyz5, "梯段宽度", 0.8, 5.0, 2.0, -XYZ.BasisY, PenColor.PC_Black);
			XYZ xyz6 = new XYZ(0.0, leftRunData.TreadDepth, 0.0);
			xyz4 = leftRunData.PtRunTopLeft - xyz6 - xyz3;
			xyz5 = xyz4 - xyz6;
			base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, xyz4, xyz5, "踏步宽", 0.8, 2.0, 5.0, -XYZ.BasisX, PenColor.PC_Black);
			if (flag)
			{
				xyz4 = rightRunData.PtRunTopRight + xyz3;
				xyz5 = xyz4 + xyz;
				base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, xyz4, xyz5, "平台宽度", 0.8, 2.0, 5.0, XYZ.BasisX, PenColor.PC_Black);
			}
			if (flag2)
			{
				XYZ xyz7 = new XYZ(param_stairs.GetStairsWellWidth(), 0.0, 0.0);
				xyz4 = leftRunData.PtRunTopRight + xyz2;
				xyz5 = xyz4 + xyz7;
				if (flag)
				{
					xyz4 += xyz;
					xyz5 += xyz;
				}
				base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, xyz4, xyz5, "梯井宽", 0.8, 5.0, 2.0, XYZ.BasisY, PenColor.PC_Black);
			}
		}

		private void DrawUpStairsNoteInfo(ref ClipperStairs param_stairs, bool bLeftSideUp)
		{
			LineRunData leftRunData = param_stairs.m_LeftRunData;
			LineRunData rightRunData = param_stairs.m_RightRunData;
			XYZ xyz = XYZ.BasisY * 1.3;
			List<UpDownText> list = new List<UpDownText>();
			string sText = "上";
			string sText2 = "下";
			if (bLeftSideUp)
			{
				XYZ pos = Geometry.CalculatMidPoint(leftRunData.PtRunBottomLeft, leftRunData.PtRunBottomRight) - xyz;
				XYZ pos2 = Geometry.CalculatMidPoint(rightRunData.PtRunTopLeft, rightRunData.PtRunTopRight) + xyz;
				XYZ pos3 = Geometry.CalculatMidPoint(leftRunData.PtRunTopLeft, leftRunData.PtRunTopRight) + xyz;
				XYZ pos4 = Geometry.CalculatMidPoint(rightRunData.PtRunBottomLeft, rightRunData.PtRunBottomRight) - xyz;
				UpDownText item = new UpDownText(pos, sText);
				list.Add(item);
				item = new UpDownText(pos2, sText);
				list.Add(item);
				item = new UpDownText(pos3, sText2);
				list.Add(item);
				item = new UpDownText(pos4, sText2);
				list.Add(item);
				base.DrawUpDownNoteInfo(ref param_stairs.m_listText, list);
				return;
			}
			XYZ pos5 = Geometry.CalculatMidPoint(leftRunData.PtRunTopLeft, leftRunData.PtRunTopRight) + xyz;
			XYZ pos6 = Geometry.CalculatMidPoint(rightRunData.PtRunBottomLeft, rightRunData.PtRunBottomRight) - xyz;
			XYZ pos7 = Geometry.CalculatMidPoint(leftRunData.PtRunBottomLeft, leftRunData.PtRunBottomRight) - xyz;
			XYZ pos8 = Geometry.CalculatMidPoint(rightRunData.PtRunTopLeft, rightRunData.PtRunTopRight) + xyz;
			UpDownText item2 = new UpDownText(pos5, sText);
			list.Add(item2);
			item2 = new UpDownText(pos6, sText);
			list.Add(item2);
			item2 = new UpDownText(pos7, sText2);
			list.Add(item2);
			item2 = new UpDownText(pos8, sText2);
			list.Add(item2);
			base.DrawUpDownNoteInfo(ref param_stairs.m_listText, list);
		}
	}
}
